package ln.fxspringarchetype.typehandler;

import ln.fxspringarchetype.util.DateUtil;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * 适用于sqlite的日期类型转换器
 * sqlite的
 */
public class BirthdayTypeHandler implements TypeHandler<Date> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Date date, JdbcType jdbcType) throws SQLException {
        ps.setString(i, DateUtil.dateToLocalDate(date).toString());
    }

    @Override
    public Date getResult(ResultSet rs, String columnName) throws SQLException {
        return DateUtil.dateStringToDate(rs.getString(columnName));
    }

    @Override
    public Date getResult(ResultSet rs, int columnIndex) throws SQLException {
        return DateUtil.dateStringToDate(rs.getString(columnIndex));
    }

    @Override
    public Date getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return DateUtil.dateStringToDate(cs.getString(columnIndex));
    }
}
